// https://www.acwing.com/problem/content/846/

#include <iostream>
#include <vector>

using namespace std;

// 这是一个超时的解法。。。。。。查了相关资料，不得不放弃 DFS

int m, n;
int board[105][105];
int visited[105][105];

int minStep = 1e7;
int to[4][2] = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}};


void dfs(int x, int y, int steps) {
    if (x == m - 1 && y == n - 1) {
        minStep = min(minStep, steps);
        return;
    }
    if (visited[x][y] || board[x][y] || steps >= minStep) {
        return;
    }
    visited[x][y] = 1;
    for (auto & t : to) {
        int tx = x + t[0];
        int ty = y + t[1];
        if (tx >= 0 && ty >= 0 && tx < m && ty < n) {
            dfs(tx, ty, steps + 1);
        }
    }
    visited[x][y] = 0;
    return;
}
/*
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

*/
int main() {
    scanf("%d%d", &m, &n);
    for (int i=0; i<m; i++) {
        for (int j=0; j<n; j++) {
            scanf("%d", &board[i][j]);
        }
    }
    dfs(0, 0, 0);
    cout<<minStep<<endl;
    return 0;
}
